package com.shuorikeji.huohuo.ui.mh;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.adapter.MhMl2Adapter;
import com.shuorikeji.huohuo.base.BasePresenterActivity;
import com.shuorikeji.huohuo.bean.MhZjDetailInfoMainBean;
import com.shuorikeji.huohuo.bean.MhZjInfoBean;
import com.shuorikeji.huohuo.bean.MhZjInfoMainBean;
import com.shuorikeji.huohuo.callback.ClickCallBack;
import com.shuorikeji.huohuo.mvp.AllPresenter;
import com.shuorikeji.huohuo.utils.ToastShowUtil;
import com.shuorikeji.huohuo.view.GridDividerItemDecoration;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class CacheMhZjActivity extends BasePresenterActivity<AllPresenter> {
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.srl_layout)
    SmartRefreshLayout srlLayout;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_cc)
    TextView tvCc;
    @BindView(R.id.tv_sec_info)
    TextView tvSecInfo;
    @BindView(R.id.ll_qx_sec)
    LinearLayout llQxSec;
    @BindView(R.id.ll_cache)
    LinearLayout llCache;

    private int MH_ID;
    private int curMhZjListPage;
    private List<MhZjInfoBean> mhZjList = new ArrayList<>();
    private MhMl2Adapter mhMl2Adapter;
    private List<MhZjInfoBean> dhcZjList = new ArrayList<>();
    private int cacheIndex;
    private List<String> mhImgList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getmImmersionBar().reset().statusBarColor(R.color.transparent).statusBarDarkFont(true, 0.2f).init();
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_cache_mh_zj);
    }

    @Override
    protected void initData() {
        curMhZjListPage = 1;
        initSmartRefreshLayout();
        initAdapter();
        initIntent();
        initClick();
    }

    private void initSmartRefreshLayout() {
        srlLayout.setRefreshHeader(new ClassicsHeader(CacheMhZjActivity.this));
        srlLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                //刷新
                curMhZjListPage = 1;
                getMhZjList();
            }
        });
        srlLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                //加载更多
                curMhZjListPage = curMhZjListPage + 1;
                getMhZjList();
            }
        });
    }

    private void initAdapter() {
        rvData.setLayoutManager(new GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false));
        rvData.addItemDecoration(new GridDividerItemDecoration(30));
        mhMl2Adapter = new MhMl2Adapter(CacheMhZjActivity.this, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        MhZjInfoBean data = (MhZjInfoBean) object;
                        for (int i = 0; i < mhZjList.size(); i++) {
                            if (data.getId() == mhZjList.get(i).getId()) {
                                if (mhZjList.get(i).isSec()) {
                                    mhZjList.get(i).setSec(false);
                                } else {
                                    mhZjList.get(i).setSec(true);
                                }
                                break;
                            }
                        }
                        mhMl2Adapter.setData(mhZjList);
                        sxCacheMhSize();
                        break;
                }
            }
        });
        rvData.setAdapter(mhMl2Adapter);
    }

    private void initIntent() {
        Intent intent = getIntent();
        MH_ID = intent.getIntExtra("MH_ID", -1);
        getMhZjList();
    }

    /**
     * 获取漫画章节列表
     */
    private void getMhZjList() {
        mPresenter.getMhZjList(true, MH_ID, 20, curMhZjListPage, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        srlLayout.finishLoadMore();
                        srlLayout.finishRefresh();
                        MhZjInfoMainBean data = (MhZjInfoMainBean) object;
                        if (curMhZjListPage == 1) {
                            mhZjList.clear();
                        }
                        mhZjList.addAll(data.getData().getChapterList().getData());
                        mhMl2Adapter.setData(mhZjList);
                        break;
                    case 2:
                        srlLayout.finishLoadMore();
                        srlLayout.finishRefresh();
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(CacheMhZjActivity.this, msg);
                        break;
                }
            }
        });
    }

    /**
     * 刷新大概缓存的大小
     */
    private void sxCacheMhSize() {
        int secCounts = 0;
        for (int i = 0; i < mhZjList.size(); i++) {
            if (mhZjList.get(i).isSec()) {
                secCounts = secCounts + 1;
            }
        }
        tvSecInfo.setText("已选择 " + secCounts + " 话，预计消耗储存大小：0MB");
    }

    private void cacheMh(MhZjInfoBean mhZjInfoBean) {
        getMhZjDetail(mhZjInfoBean.getId());
    }

    private void getMhZjDetail(int zjId) {
        mPresenter.getMhZjDetail(true, MH_ID, zjId, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        MhZjDetailInfoMainBean data = (MhZjDetailInfoMainBean) object;
                        mhImgList.clear();
                        mhImgList.addAll(data.getData().getChapter().getImages());
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(CacheMhZjActivity.this, msg);
                        break;
                }
            }
        });
    }

    private void initClick() {
        ivBack.setOnClickListener(view -> {
            finish();
        });
        llQxSec.setOnClickListener(view -> {
            if (mhZjList != null && mhZjList.size() > 0) {
                for (int i = 0; i < mhZjList.size(); i++) {
                    mhZjList.get(i).setSec(false);
                }
                mhMl2Adapter.setData(mhZjList);
                tvSecInfo.setText("已选择 0 话，预计消耗储存大小：0MB");
            }
        });
        llCache.setOnClickListener(view -> {
            dhcZjList.clear();
            for (int i = 0; i < mhZjList.size(); i++) {
                if (mhZjList.get(i).isSec()) {
                    dhcZjList.add(mhZjList.get(i));
                }
            }
            if (dhcZjList != null && dhcZjList.size() > 0) {
                cacheIndex=0;
                cacheMh(dhcZjList.get(cacheIndex));
            } else {
                ToastShowUtil.showToastCenter(CacheMhZjActivity.this, "请选择要缓存的章节");
            }
        });
    }
}